< previous page page_415 next page >

Page 415
Adding Data at Runtime
You can perform the same task at runtime using Visual Basic code. Add a command button to this form, setting its Name property to cmdSales and its Caption property to Sales. Then add the code in Listing 11.1 in the cmdSales_Click event:
Type.gifListing 11.1. Adding code to the cmdSales_Click event.
Private Sub cmdSales_Click()
    '
    ' add data using VB code
    '
    Dim intLoop As Integer
    '
    Graph1.DataReset = gphAllData   ' clear out all properties
    Graph1.GraphType = gphBar3D     ' use 3d bar chart
    '
    Graph1.NumSets = 1  ' only one set of data
    Graph1.NumPoints = 12 ' one for each month
    '
    ' now add the data
    '
    For intLoop = 1 To 12
        Graph1.ThisPoint = intLoop  ' point to fill
        Graph1.GraphData = intLoop  ' data to load
    Next
    '
    Graph1.DrawMode = gphDraw ' show results
    '
End Sub
In Listing 11.1, you do a few things. First, you clear out any data that might already be stored in the graph control. Next, you set the GraphType property to show a three-dimensional bar graph. Also, you set the NumSets and NumPoints properties to 1 and 12, respectively, and then add the data points. Notice that the graph control must be told which set you want filled (using the ThisSet property). Next, you go through a loopfirst, setting the ThisPoint property and then adding the data item. Finally, you set the DrawMode property to gphDraw to force Visual Basic to redraw the graph with the new data.
Save and run the project. When you click the Sales button, your form looks similar to the one shown in Figure 11.5.
Although this method works well, there is a faster method. By setting the AutoInc property of the graph control to 1, the graph control automatically increments the NumPoints property. This can simplify and speed up your code. Add a new button to the form, setting its Name property to cmdAutoSales and its Caption property to &AutoSales. Then add the code in Listing 11.2 in the cmdAutoSales_Click event.

 
< previous page page_415 next page >

If you like this book, buy it!